﻿using GetSlaughterItem.API.Application.Commands.Week03.leos;
using MediatR;
using SlaughterItem.Common.Domains.Saleser;
using SlaughterItem.Common.Domains.UPCTable;
using SlaughterItem.Common.Domains.Warehouse;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week03.wenk
{
    public class OilyfatScrentenHandler : IRequestHandler<OilyfatScrentenCommend, Result>
    {
        private readonly IBaseRepositoryGet<Oilyfat> Oilyfat;
        private readonly IBaseRepositoryGet<DepotType> DepotType;
        private readonly IBaseRepositoryGet<UPCData> UPCData;

        public OilyfatScrentenHandler(IBaseRepositoryGet<Oilyfat> Oilyfat, IBaseRepositoryGet<UPCData> UPCData,
            IBaseRepositoryGet<DepotType> DepotType)
        {
            this.Oilyfat = Oilyfat;
            this.DepotType = DepotType;
            this.UPCData = UPCData;
        }

        public async Task<Result> Handle(OilyfatScrentenCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };

            try
            {
                var list = (from a in Oilyfat.GetAll()
                            join b in UPCData.GetAll() on a.GoodsMinuteId equals b.GoodsMinuteId
                            join c in DepotType.GetAll() on a.DepotTypeId equals c.DepotTypeId
                            where a.IsDelete == false
                            select new
                            {
                                a.OilyfatId,
                                a.OilyfatPlanNum,
                                a.OilyfatTime,
                                a.OilyfatNum,
                                a.OilyfatGrossWeight,
                                a.OilyfatTares,
                                a.OilyfatWeight,
                                a.OilyfatRemark,
                                b.GoodsMinuteId,
                                b.UPCDataName,
                                c.DepotTypeId,
                                c.DepotTypeName,
                            }).ToList();


                var totalcount = list.Count();
                var pagecount = (int)Math.Ceiling((totalcount * 1.0) / request.pageSize);
                list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

                var lief = new
                {
                    totalcount,
                    pagecount,
                    list,
                };

                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = lief;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
